package com.ubtechinc.cruzr.launcher2.model.helper;

import com.ubtechinc.cruzr.launcher2.LauncherApplication;
import com.ubtechinc.cruzr.launcher2.callback.AdminiListener;
import com.ubtechinc.cruzr.launcher2.model.AppManger;
import com.ubtechinc.cruzr.launcher2.utlis.LogUtils;
import com.ubtechinc.cruzr.userverify.api.CruzrSecurityApi;
import com.ubtechinc.cruzr.userverify.api.ISecurityCallBack;

/**
 * Created by ruiqiang on 2018/5/10.
 */

public class AdministratorObserve {
    private boolean isAdminiInit = false;

    public void initSecurityApi(final AdminiListener listener) {
        CruzrSecurityApi.get().init(LauncherApplication.getContext(), new ISecurityCallBack() {
            @Override
            public void securityModeChange(int i) {
                if (listener != null) {
                    if (i == CruzrSecurityApi.MODE_CHANGE_CODE_VISITOR) {
                        listener.onExitAdmini();
                    } else {
                        listener.onEnterAdmini();
                    }
                }
            }

            @Override
            public void onBindSuccess() {
                if (!isAdminiInit) {
                    AppManger.get().loadApps(true, isAdmini());
                }
                isAdminiInit = true;
                LogUtils.e("admini  onBindSuccess");
            }

            @Override
            public void onBindfail() {

            }

            @Override
            public void onDisconnect() {
                if (listener != null) listener.onExitAdmini();
            }
        });
    }

    public boolean isAdminiInit() {
        return isAdminiInit;
    }

    public boolean isAdmini() {
        return CruzrSecurityApi.get().getAdminiMode() != CruzrSecurityApi.MODE_CHANGE_CODE_VISITOR;
    }
}
